package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.dto.SkuSalesDto;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import com.atguigu.gmall.sms.service.SkuFullReductionService;
import com.atguigu.gmall.sms.vo.ItemSaleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.sms.mapper.SkuLadderMapper;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.service.SkuLadderService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuLadderService")
public class SkuLadderServiceImpl extends ServiceImpl<SkuLadderMapper, SkuLadderEntity> implements SkuLadderService {
    @Autowired
    SkuBoundsService skuBoundsService;
    @Autowired
    SkuFullReductionService skuFullReductionService;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuLadderEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuLadderEntity>()
        );

        return new PageResultVo(page);
    }
    @Transactional
    @Override
    public void saveSkuSales(SkuSalesDto skuSalesDto) {
        //拆分skuSalesDto数据到三张营销表对象中
        //1、积分数据：
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        skuBoundsEntity.setGrowBounds(new BigDecimal(skuSalesDto.getGrowBounds()));
        skuBoundsEntity.setBuyBounds(new BigDecimal(skuSalesDto.getBuyBounds().intValue()));
        skuBoundsEntity.setSkuId(skuSalesDto.getSkuId());
        //[ 0 0 1 0]
        Integer[] work = skuSalesDto.getWork();
        skuBoundsEntity.setWork(work[0]*1+work[1]*2+work[2]*4+work[3]*8);
        skuBoundsService.save(skuBoundsEntity);

        //2、折扣数据：
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setFullCount(skuSalesDto.getFullCount());
        skuLadderEntity.setDiscount(skuSalesDto.getDiscount());
        skuLadderEntity.setAddOther(skuSalesDto.getLadderAddOther());
        skuLadderEntity.setSkuId(skuSalesDto.getSkuId());
        this.save(skuLadderEntity);
        //3、满减数据
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setFullPrice(skuSalesDto.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuSalesDto.getReducePrice());
        skuFullReductionEntity.setAddOther(skuSalesDto.getFullAddOther());
        skuFullReductionEntity.setSkuId(skuSalesDto.getSkuId());
        skuFullReductionService.save(skuFullReductionEntity);
    }

    @Override
    public List<ItemSaleVo> querySalesBySkuId(String skuId) {
        //查询折扣表的营销数据
        SkuLadderEntity skuLadderEntity = this.getOne(new LambdaQueryWrapper<SkuLadderEntity>()
                .eq(SkuLadderEntity::getSkuId, skuId)
                .ne(SkuLadderEntity::getDiscount,0));

        //查询满减表的营销数据
        SkuFullReductionEntity skuFullReductionEntity = skuFullReductionService.getOne(new LambdaQueryWrapper<SkuFullReductionEntity>()
                .eq(SkuFullReductionEntity::getSkuId, skuId)
                .ne(SkuFullReductionEntity::getFullPrice , 0)
                .ne(SkuFullReductionEntity::getReducePrice, 0));
        //查询积分表的营销数据
        SkuBoundsEntity skuBoundsEntity = skuBoundsService.getOne(new LambdaQueryWrapper<SkuBoundsEntity>()
                .eq(SkuBoundsEntity::getSkuId, skuId)
                .ne(SkuBoundsEntity::getBuyBounds , 0)
                .ne(SkuBoundsEntity::getGrowBounds , 0));
        List<ItemSaleVo> itemSaleVos = new ArrayList<>();
        //将上面的营销对象转为ItemSaleVo对象
        if(skuLadderEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuLadderEntity.getId().toString());
            itemSaleVo.setType("折扣");
            itemSaleVo.setDesc("满 "+skuLadderEntity.getFullCount()+
                    " 件打"+skuLadderEntity.getDiscount()+"% 折");
            itemSaleVos.add(itemSaleVo);
        }
        if(skuFullReductionEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuFullReductionEntity.getId().toString());
            itemSaleVo.setType("满减");
            itemSaleVo.setDesc("满 "+skuFullReductionEntity.getFullPrice() +
                    " 元减 "+skuFullReductionEntity.getReducePrice()+" 元");
            itemSaleVos.add(itemSaleVo);
        }
        if(skuBoundsEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuBoundsEntity.getId().toString());
            itemSaleVo.setType("赠送积分");
            itemSaleVo.setDesc("送 "+skuBoundsEntity.getBuyBounds() +
                    " 购物积分,送 "+skuBoundsEntity.getGrowBounds()+" 成长积分");
            itemSaleVos.add(itemSaleVo);
        }

        return itemSaleVos;
    }

}